package com.wmlive.hhvideo.heihei.mainhome.presenter;

import android.text.TextUtils;

import com.wmlive.hhvideo.common.base.BasePresenter;
import com.wmlive.hhvideo.common.base.BaseView;
import com.wmlive.hhvideo.common.network.HttpConstant;
import com.wmlive.hhvideo.heihei.beans.main.Comment;
import com.wmlive.hhvideo.heihei.beans.main.CommentDataCount;
import com.wmlive.hhvideo.heihei.beans.main.CommentDeleteResponse;
import com.wmlive.hhvideo.heihei.beans.main.VideoCommentListResponse;
import com.wmlive.hhvideo.heihei.beans.splash.InitCatchData;
import com.wmlive.networklib.entity.DCNetObserver;

import java.util.List;

/**
 * Created by lsq on 6/19/2017.
 * 评论列表页面
 */

public class CommentListPresenter extends BasePresenter<CommentListPresenter.ICommentListView> {
    private int commentOffset;//评论列表的offset

    public CommentListPresenter(ICommentListView view) {
        super(view);
    }

    //获取评论的列表
    public void getCommentList(final boolean isRefresh, long videoId) {
        String url = InitCatchData.opusListOpusComment();
        if (!TextUtils.isEmpty(url)) {
            executeRequest(HttpConstant.TYPE_COMMENT_LIST, getHttpApi().fetchCommentList(url, videoId, commentOffset = (isRefresh ? 0 : commentOffset)))
                    .subscribe(new DCNetObserver<VideoCommentListResponse>() {
                        @Override
                        public void onRequestDataReady(int requestCode, String message, VideoCommentListResponse response) {
                            if (null != viewCallback) {
                                commentOffset = response.getOffset();
                                viewCallback.onCommentListOk(isRefresh, response.getComments(), response.isHas_more());
                            }
                        }

                        @Override
                        public void onRequestDataError(int requestCode, int serverCode, String message) {
                            onRequestError(isRefresh ? HttpConstant.TYPE_COMMENT_LIST : HttpConstant.TYPE_COMMENT_LIST + 1, message);
                        }
                    });

        } else {
            onRequestUrlError(HttpConstant.TYPE_COMMENT_LIST);
        }
    }

    //删除评论
    public void deleteComment(final int position, final long commentId, long opusId) {
        String url = InitCatchData.opusDeleteComment();
        if (!TextUtils.isEmpty(url)) {
            executeRequest(HttpConstant.TYPE_COMMENT_DELETE, getHttpApi().deleteComment(url, commentId, opusId))
                    .subscribe(new DCNetObserver<CommentDeleteResponse>() {
                        @Override
                        public void onRequestDataReady(int requestCode, String message, CommentDeleteResponse response) {
                            if (viewCallback != null) {
                                viewCallback.onDeleteCommentOk(position, commentId, response.getData_count());
                            }
                        }

                        @Override
                        public void onRequestDataError(int requestCode, int serverCode, String message) {
                            onRequestError(HttpConstant.TYPE_COMMENT_DELETE, message);
                        }
                    });
        } else {
            onRequestUrlError(HttpConstant.TYPE_COMMENT_DELETE);
        }
    }

    public interface ICommentListView extends BaseView {
        void onCommentListOk(boolean isRefresh, List<Comment> list, boolean hasMore);

        void onDeleteCommentOk(int position, long commentId, CommentDataCount countBean);
    }
}
